package ren.nearby.http.utils.util;

import com.orhanobut.logger.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class FMD5Utils {
    public static String MD5(String str) {
        try {
            // Create MD5 Hash
            MessageDigest digest = MessageDigest
                    .getInstance("MD5");
            digest.update(str.getBytes());
            byte messageDigest[] = digest.digest();
            return toHexString(messageDigest).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";

    }

    private static String toHexString(byte[] b) { // String to byte
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            int j = 0xFF & b[i];
            if (j <= 15) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(j));
        }
        return sb.toString();
    }

//    private static final String KEY = "b842bfb5875dc5b5edddc2348c563c7c";
    private static final String KEY = "whNQTlSuw3xZW2kva5ovgyvsQrDGrGMZ";

    public static String getAccessToken(TreeMap<String, String> paramTreeMap) {
        String str1 = "";
        Iterator localIterator = paramTreeMap.keySet().iterator();
        while (localIterator.hasNext()) {
            String str3 = (String) localIterator.next();
            str1 = str1 + str3 + "=" + (String) paramTreeMap.get(str3) + "&";
        }
        String str2 = str1.substring(0, -1 + str1.length());
        return digest(str2 + KEY);
    }

    static final String HEXES = "0123456789abcdef";

    public static String digest(String paramString) {
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("md5");
            localMessageDigest.update(paramString.getBytes());
            String str = getHex(localMessageDigest.digest());
            return str;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return null;
    }


    private static String getHex(byte[] paramArrayOfByte) {
        if (paramArrayOfByte == null)
            return null;
        StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
        int i = paramArrayOfByte.length;
        for (int j = 0; j < i; j++) {
            int k = paramArrayOfByte[j];
            localStringBuilder.append(HEXES.charAt((k & 0xF0) >> 4)).append(HEXES.charAt(k & 0xF));
        }
        return localStringBuilder.toString();
    }

    /**
     * 使用map按Key排序
     *
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {

        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<String, String>(
                new MapKeyComparator());
        sortMap.putAll(map);
        Logger.d("sortMap = "+sortMap.size());
        return sortMap;
    }

    public static String subuString(Map<String, String> map) {
        StringBuffer queryStr = new StringBuffer("");
//        Map<String, String> resultMap = sortMapByKey(map);
        Iterator<String> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
          /*  if (queryStr.toString().equals("")) {
                queryStr.append(key + "=" + resultMap.get(key));
            } else {
                queryStr.append("&" + key + "=" + resultMap.get(key));
            }*/


                queryStr.append( map.get(key));

        }
        Logger.d("queryStr = "+queryStr.toString());

        return queryStr.toString();
    }

    static class MapKeyComparator implements Comparator<String> {
        @Override
        public int compare(String lhs, String rhs) {
            return lhs.compareTo(rhs);
        }
    }

}

